import provinces from './china-division/provinces';
import cities from './china-division/cities';
import areas from './china-division/areas';

let staticProvinces = provinces;

const generateOptions = (flag) => {

  areas.forEach((area) => {
    let matchCity = cities.filter(city => city.code === area.cityCode)[0];
    flag || (matchCity = null);
    if (matchCity) {
      matchCity.children = matchCity.children || [];
      matchCity.children.push({
        label: area.name,
        value: area.code,
      });
    }
  });


  provinces[0].children ||
  cities.forEach((city) => {
    const matchProvince = staticProvinces.filter(province => province.code === city.provinceCode)[0];
    if (matchProvince) {
      matchProvince.children = matchProvince.children || [];
      matchProvince.children.push({
        label: city.name,
        value: city.code,
        children: city.children,
      });
    }
  });

  const options = staticProvinces.map(province => ({
    label: province.name,
    value: province.code,
    children: province.children,
  }));

  return options;
};

export default generateOptions;